﻿//-------------------------------------------------------------------------------------------
// <copyright file="CloseableViewModel.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.TestClient.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;

    /// <summary>
    /// A ViewModel with a CloseCommand handler.
    /// </summary>
    public abstract class CloseableViewModel<TModel> : ViewModel<TModel>
        where TModel : class, INotifyPropertyChanged
    {
        private DelegateCommand closeCommand;

        protected CloseableViewModel()
            : base()
        {
        }

        /// <summary>
        /// Raised when this workspace should be removed from the UI.
        /// </summary>
        public event EventHandler RequestClose;

        /// <summary>
        /// Returns the command that, when invoked, attempts
        /// to remove this workspace from the user interface.
        /// </summary>
        public ICommand CloseCommand
        {
            get
            {
                if (this.closeCommand == null)
                {
                    this.closeCommand = new DelegateCommand(param => this.OnRequestClose(), param => this.CanClose());
                }

                return this.closeCommand;
            }
        }

        protected virtual bool CanClose()
        {
            return true;
        }

        private void OnRequestClose()
        {
            EventHandler handler = this.RequestClose;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }
    }
}